package com.ruoyi.miaomiao.controller.anonymous;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.miaomiao.domain.AdvertismentPicture;
import com.ruoyi.miaomiao.domain.FGoods;
import com.ruoyi.miaomiao.domain.SlidePicture;
import com.ruoyi.miaomiao.service.IAdvertismentPictureService;
import com.ruoyi.miaomiao.service.IFGoodsService;
import com.ruoyi.miaomiao.service.IHotGoodsService;
import com.ruoyi.miaomiao.service.ISlidePictureService;
import com.ruoyi.miaomiao.util.ServiceUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务于用户的商品、轮播图、宣传图的查询接口
 */
@Api(tags = "商品、轮播图、宣传图查询") //  tags：你可以当作是这个组的名字。
@RestController
@RequestMapping("/goods")
@ApiResponses(value = {@ApiResponse(code = 0, message = "商品对象信息", response = FGoods.class),
        @ApiResponse(code = 0, message = "轮播图对象信息", response = SlidePicture.class),
        @ApiResponse(code = 0, message = "宣传图对象信息", response = AdvertismentPicture.class)})
public class AnGoodsController extends BaseController {
    @Autowired
    private IFGoodsService fGoodsService;
    @Autowired
    private IHotGoodsService hotGoodsService;
    @Autowired
    private ISlidePictureService slidePictureService;
    @Autowired
    private IAdvertismentPictureService advertismentPictureService;
    @Autowired
    private ServiceUtil serviceUtil;

    /**
     * 查询商品列表
     */
    @ApiOperation(value = "查询商品列表", notes = "可选上传queryParams数据（键值对）并按照所填写字段做交集查询")
    @GetMapping("/list")
    public TableDataInfo list(FGoods fGoods) {
        startPage();
        List<FGoods> list = fGoodsService.selectFGoodsList(fGoods);
        for (FGoods fGood : list) {
            fGood.setGoodsCover(serviceUtil.getCurrentServiceUrl() + "/image/cover/" + fGood.getGoodsCover());
        }
        return getDataTable(list);
    }

    /**
     * 获取商品详细信息
     */
    @ApiOperation(value = "获取商品详细信息", notes = "按商品查询单个商品所有信息")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId) {
        FGoods fGood = fGoodsService.selectFGoodsByGoodsId(goodsId);
        fGood.setGoodsCover(serviceUtil.getCurrentServiceUrl() + "/image/cover/" + fGood.getGoodsCover());
        return AjaxResult.success(fGood);
    }

    /**
     * 默认获取前20个热销商品列表
     */
    @ApiOperation(value = "获取前20个热销商品列表与", notes = "返回20个热销商品列表")
    @GetMapping("/hot")
    public TableDataInfo getHotGoods() {
        Long[] goodIds = hotGoodsService.selectTwentyHotGoodsList();
        List<FGoods> fGoods = fGoodsService.selectFGoodsListByIds(goodIds);
        for (FGoods fGood : fGoods) {
            fGood.setGoodsCover(serviceUtil.getCurrentServiceUrl()+"/image/cover/"+fGood.getGoodsCover());
        }
        return getDataTable(fGoods);
    }

    /**
     * 获取轮播图列表
     */
    @ApiOperation(value = "获取轮播图", notes = "返回全部（4张）轮播图及轮播图对应商品的id及其他信息")
    @GetMapping("/hot/slide")
    public TableDataInfo getSlidePic(SlidePicture slidePicture) {
        List<SlidePicture> SlidePic = slidePictureService.selectSlidePictureList(slidePicture);
        for (SlidePicture pic : SlidePic) {
            pic.setSlidepictureCover(serviceUtil.getCurrentServiceUrl() + "/image/slide/" + pic.getSlidepictureCover());
        }
        return getDataTable(SlidePic);
    }

    /**
     * 获取宣传图
     * @param advertismentPicture
     * @return TableDataInfo
     */
    @ApiOperation(value = "获取宣传图", notes = "返回全部宣传图信息")
    @GetMapping("/hot/adverts")
    public TableDataInfo getAdvertPic(AdvertismentPicture advertismentPicture) {
        List<AdvertismentPicture> AdvertismentPicture = advertismentPictureService.selectAdvertismentPictureList(advertismentPicture);
        for (AdvertismentPicture pic : AdvertismentPicture) {
            pic.setAdvertismentPictureCover(serviceUtil.getCurrentServiceUrl() + "/image/adverts/" + pic.getAdvertismentPictureCover());
        }
        return getDataTable(AdvertismentPicture);
    }


    /**
     * 按量获取热销商品列表
     * @param num 限定返回热销商品列表的长度
     * @return TableDataInfo
     */
    @ApiOperation(value = "按量获取热销商品列表", notes = "通过指定数量按量获取热销商品列表")
    @GetMapping(value = "/hot/{num}")
    public TableDataInfo getHotGoodsByNum(@PathVariable("num") Integer num) {
        //当前端传值小于0时，默认返回20个热销商品
        if (num <= 0) {
            return getHotGoods();
        }
        Long[] goodIds = hotGoodsService.selectHotGoodsListByNum(num);
        List<FGoods> fGoods = fGoodsService.selectFGoodsListByIds(goodIds);
        for (FGoods fGood : fGoods) {
            fGood.setGoodsCover(serviceUtil.getCurrentServiceUrl() + "/image/" + fGood.getGoodsCover());
        }
        return getDataTable(fGoods);
    }

}